<template>
  <div id="dl">
    <h2>注册</h2>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账户" prop="ucode">
        <el-input v-model="ruleForm.ucode"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="upassword">
        <el-input v-model="ruleForm.upassword"></el-input>
      </el-form-item>
      <el-form-item label="确定密码" prop="upassword1">
        <el-input v-model="ruleForm.upassword1"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        ucode: "",
        upassword: "",
        upassword1: "",
      },
      rules: {
        ucode: [
          { required: true, message: "账户不为空！", trigger: "blur" },
          {
            min: 3,
            max: 8,
            message: "账户长度在 3 到 8 个字符",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              this.axios
                .get("http://localhost:8086/login.do/cha", {
                  params: { sysuser: this.ruleForm.ucode },
                })
                .then((res) => {
                  if (res.data) {
                    return callback(
                      new Error("账户 " + this.ruleForm.ucode + " 已存在！")
                    );
                  } else {
                    return callback();
                  }
                });
            },
            trigger: "blur",
          },
        ],
        upassword: [
          { required: true, message: "新密码不能为空!", trigger: "blur" },
          {
            min: 3,
            max: 8,
            message: "密码长度在 3 到 8 个字符",
            trigger: "blur",
          },
        ],
        upassword1: [
          { required: true, message: "确定密码不能为空!", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value != this.ruleForm.upassword) {
                return callback(new Error("新密码和确定密码不一致!"));
              } else {
                return callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .post("http://localhost:8086/login.do/add", this.ruleForm)
            .then((res) => {
              if (res.data) {
                alert("注册成功！");
              } else {
                alert("注册失败！");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
#dl {
  text-align: center;
  width: 20%;
  margin-left: 30%;
}
</style>